package com.atguigu.gmall.realtime.app.marketing.func;

import com.atguigu.gmall.realtime.app.marketing.bean.GmallEvent;
import com.atguigu.gmall.realtime.app.marketing.bean.MarketingMessage;
import com.atguigu.gmall.realtime.app.marketing.bean.MarketingRuleAllInfo;

import static com.atguigu.gmall.realtime.app.marketing.constant.MarketingConst.*;

public class MessageBuilder {


    public MarketingMessage  build(MarketingRuleAllInfo marketingRuleAllInfo, GmallEvent event){
        MarketingMessage marketingMessage = new MarketingMessage();
        marketingMessage.setRuleId(marketingRuleAllInfo.getId());
        marketingMessage.setText(marketingRuleAllInfo.getModelText());
        marketingMessage.setTitle(marketingRuleAllInfo.getModelTitle());
        marketingMessage.setType(marketingRuleAllInfo.getChannelType());

        if(event.getTopic().equals(TOPIC_START)
                ||event.getTopic().equals(TOPIC_ACTION)
                ||event.getTopic().equals(TOPIC_PAGE)) {
            String uid = event.getData().getJSONObject("common").getString("uid");
            marketingMessage.setUid(uid);
        }
        marketingMessage.setLimitTimeNum(marketingRuleAllInfo.getLimitTimeNum());
        marketingMessage.setLimitTimeUnit(marketingRuleAllInfo.getLimitTimeUnit());
        marketingMessage.setUrl(marketingRuleAllInfo.getUrl());
        return marketingMessage;
    }

}
